import { base } from "@/utils/config";
import request from "@/utils/request";
import { getToken } from '@/utils/auth';

// 获取本地文件
export const LocalFile = params => request('/api/v1/SystemBasis/LocalFile', { params: params, responseType: 'blob' });

// word转图片
export const WordToImage = (data) => {
  return request({
    url: `${base}/api/v1/SystemBasis/WordToImage`,
    method: "get",
    params: data,
  });
};

// 上传视频
export const uploadVideoFile = (data) =>
  request.post(`${base}/api/v1/SystemBasis/UploadVideo`, data, {
    headers: { "Content-Type": "multipart/form-data" },
  });
// 上传图片
export const uploadFile = `${base}/api/v1/PermissionUser/UplodeFile`;
export const uploadImgFile = (data) =>
  request.post(`${base}/api/v1/SystemBasis/UploadImage`, data, {
    headers: { "Content-Type": "multipart/form-data" },
  });
// 保存旋转之后得图片
export const rotateImg = data => request.post(`${base}/api/v1/SystemBasis/rotateImg`, data);
// 上传文件
export const uploadFileInfo = (data) =>
  request.post(`${base}/api/v1/SystemBasis/UploadWorkFile`, data, {
    headers: { "Content-Type": "multipart/form-data" },
    timeout: 1000000,
  });

// 获取公司接口基本信息
export const GetCompanyMaterial = (data) => {
  return request({
    url: `${base}/api/v1/SystemDict/GetCompanyMaterial`,
    method: "get",
    params: data,
  });
};

// 获取静态资源 返回图片 的base64字符串
export const GetStaticImgInfo = (fileName) => {
  return request({
    url: `${base}/api/v1/SystemBasis/GetImgInfo?file=` + fileName,
    method: "get",
  });
};
export const GetPublicStaticImgInfo = (data) => {
  return request({
    url: `${base}/api/v1/SystemBasis/GetImgInfo`,
    method: "get",
    params: data,
  });
};

// 通用的下载文件的方法,添加了请求头
function getStaticFileBlob(fileName) {
  var url = `${base}/api/v1/SystemBasis/GetFileInfo?file=` + fileName;
  return new Promise((resolve) => {
    const xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.responseType = "blob";
    xhr.setRequestHeader("Authorization", getToken())
    xhr.onload = () => {
      if (xhr.status === 200) {
        resolve(xhr.response);
      }
    };
    xhr.send();
  });
}

// 下载文件js模拟点击a标签进行下载
function getStaticFileBlobSaveAs(blob, filename) {
  var link = document.createElement("a");
  link.href = window.URL.createObjectURL(blob);
  link.download = filename;
  link.click();
}
// 获取静态资源 返回文件流
// originFileName: 原始文件名 下载时显示的文件名,如果不传,则使用originFileName
export const GetStaticFileInfo = (originFileName, newFileName) => {
  if (originFileName) {
    getStaticFileBlob(originFileName).then((blob) => {
      if (newFileName) {
        getStaticFileBlobSaveAs(blob, newFileName);
      } else {
        getStaticFileBlobSaveAs(blob, originFileName);
      }
    });
  }
};

// 传入文件名,返回具体文件的下载路径url
export const GetFileInfoUrl = (fileName) => {
  return `${base}/api/v1/SystemBasis/GetFileInfo?file=` + fileName;
}

export default {
  uploadFile,
  uploadImgFile,
  uploadFileInfo,
  GetStaticImgInfo,
  GetStaticFileInfo,
  GetPublicStaticImgInfo,
  GetFileInfoUrl,
};
